package com.demo;

import java.util.*;

public class Demo03 {

    public static List<Item> mergeItems(List<Map<String, Object>> inputList) {
        Map<BizAmountKey, Integer> countMap = new HashMap<>();

        for (Map<String, Object> map : inputList) {
            String bizProd = (String) map.get("bizProd");
            Integer amount = (Integer) map.get("amount");

            BizAmountKey key = new BizAmountKey(bizProd, amount);
            countMap.put(key, countMap.getOrDefault(key, 0) + 1);
        }

        List<Item> result = new ArrayList<>();
        for (Map.Entry<BizAmountKey, Integer> entry : countMap.entrySet()) {
            BizAmountKey key = entry.getKey();
            int count = entry.getValue();
            result.add(new Item(key.bizProd, key.amount, count));
        }

        return result;
    }

    public static void main(String[] args) {
        List<Map<String, Object>> input = new ArrayList<>();
       
        Map<String, Object> map1 = new HashMap<>();
        map1.put("bizProd", "A");
        map1.put("amount", 100);
        input.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("bizProd", "A");
        map2.put("amount", 200);
        input.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("bizProd", "B");
        map3.put("amount", 200);
        input.add(map3);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("bizProd", "C");
        map4.put("amount", 150);
        input.add(map4);

        Map<String, Object> map5 = new HashMap<>();
        map5.put("bizProd", "C");
        map5.put("amount", 200);
        input.add(map5);

        Map<String, Object> map6 = new HashMap<>();
        map6.put("bizProd", "A");
        map6.put("amount", 100);
        input.add(map6);

        List<Item> merged = mergeItems(input);

        for (Item item : merged) {
            System.out.println(item);
        }
    }

}
